const {ccclass, property} = cc._decorator;

@ccclass
export default class SceneTransition extends cc.Component
{
    @property(cc.Animation)
    animation:cc.Animation = null;
    @property
    inAnimationClip: string = "";
    @property
    outAnimationClip: string = "";

    static instance:SceneTransition = null;

    start () {
        SceneTransition.instance = this;
    }
    /**
     * 播放切入动画
     * @param cb 切入动画播放完毕后的回调
     */
    playIn(cb) {
        this.animation.node.active = true;
        this.animation.getAnimationState(this.inAnimationClip).duration = 1.5;
        this.animation.play(this.inAnimationClip)
        this.animation.once(cc.Animation.EventType.FINISHED, cb);
    }

    /**
     * 播放切出动画
     */
    playOut() {
        this.animation.getAnimationState(this.outAnimationClip).duration = 1.5;
        this.animation.play(this.outAnimationClip);
        this.animation.once(cc.Animation.EventType.FINISHED, ()=>{
            this.animation.node.active = false;
        })
    }

    loadScene(name) {
        cc.log("SceneTransition|loadScene|" + this.animation.node.activeInHierarchy)
        this.playIn(() => {
            cc.director.loadScene(name, () => {
                this.playOut();
            });
        });
    }
}